<?php

namespace App\Api\Controllers;

use App\Api\Manages\Common;

use App\Api\Repositories\ApiFace;
use App\Api\Services\ApibaiduFace;
use App\Api\Services\JsonService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;


class ApiFaceController extends Controller
{

    /**
     * 人脸添加
     * @param array $data
     * @return array
     */
    public function saveFace(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'file' => 'required|image',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $path = $params['file']->getRealPath();
        $img = base64_encode(file_get_contents($path));
        $data =['image'=>$img];
        $result = ApiFace::saveFace($data);
        if ($result && $result['error_code'] == 0) {
            return JsonService::success('添加成功',$result['result']);
        } else {
            return JsonService::fail('添加失败');
        }
    }

    /**
     * 人脸更新
     * @param array $data
     * @return array
     */
    public function updateFace(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'file' => 'required|image',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $path = $params['file']->getRealPath();
        $img = base64_encode(file_get_contents($path));
        $result = ApiFace::updateFace($img);
        if ($result && $result['error_code'] == 0) {
            return JsonService::success('更新成功',$result['result']);
        } else {
            return JsonService::fail('更新失败');
        }
    }



}
